package com.qf.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.SpecificationsMapper;
import com.qf.entitys.Specifications;
import com.qf.service.SpecificationsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SpecificationsServiceImpl implements SpecificationsService {

    @Resource
    private SpecificationsMapper mapper;

    @Override
    public int deleteByPrimaryKey(Integer sid) {
        return mapper.deleteByPrimaryKey(sid);
    }

    @Override
    public int insert(Specifications record) {
        return 0;
    }

    @Override
    public int insertSelective(Specifications record) {
        return mapper.insertSelective(record);
    }

    @Override
    public Specifications selectByPrimaryKey(Integer sid) {
        return mapper.selectByPrimaryKey(sid);
    }

    @Override
    public int updateByPrimaryKeySelective(Specifications record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Specifications record) {
        return 0;
    }

    @Override
    public PageInfo<Specifications> selectAll(Integer pageindex,Specifications specifications) {
        PageHelper.startPage(pageindex,10);
        List<Specifications> specification = mapper.selectAll(specifications);
        PageInfo<Specifications> specificationsPageInfo = new PageInfo<>(specification);
        return specificationsPageInfo;
    }

    /**
     * 批量删除，通过事务来提交，若出现异常则回滚删除操作
     * @param ids
     * @return
     */
    @Override
    public int deleteByIds(Integer[] ids) {
        if (ids.length<=0) {
            return 0;
        }
        for (Integer id : ids) {

            int i = deleteByPrimaryKey(id);
            if (i==0){
                //出现错误就向上抛异常，事务默认遇到运行时异常就会回滚操作
                throw new RuntimeException("删除失败");
            }
        }
        //如果没问题就返回1
        return 1;
    }


}
